package stockService;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class loadData {
    public String code;
    public String name;
    public String today;
    public String last;
    public String current;
    public String highest;
    public String lowest;

    public String quan;
    public String price;

    public String[] bquan;
    public String[] bprice;

    public String[] squan;
    public String[] sprice;

    public float perc;

    public static loadData[] stocks = new loadData[]{new loadData("0000"), new loadData("0000"), new loadData("0000"), new loadData("0000"), new loadData("0000")};
    public static int count = 0;

    public static int cur = 0;
    public static int img = 0;

    loadData(String code) {
        this.code = code;
        this.name = "Error Stock";
        this.today = "0.000";
        this.last = "0.000";
        this.current = "0.000";
        this.highest = "0.000";
        this.lowest = "0.000";
        this.quan = "0";
        this.price = "0.000";
        this.bquan = new String[]{"0", "0", "0", "0", "0"};
        this.bprice = new String[]{"0.000", "0.000", "0.000", "0.000", "0.000"};
        this.squan = new String[]{"0", "0", "0", "0", "0"};
        this.sprice = new String[]{"0.000", "0.000", "0.000", "0.000", "0.000"};
        this.perc = 0;
    }

    loadData(String code, String name, String today, String last, String current, String highest, String lowest, String quan, String price, String[] bquan, String[] bprice, String[] squan, String[] sprice) {
        this.code = code;
        this.name = name;
        this.today = today;
        this.last = last;
        this.current = current;
        this.highest = highest;
        this.lowest = lowest;
        this.quan = quan;
        this.price = price;
        this.bquan = bquan;
        this.bprice = bprice;
        this.squan = squan;
        this.sprice = sprice;
        this.perc = (Float.parseFloat(current) - Float.parseFloat(last)) / Float.parseFloat(last);
    }


    public static void setStock(String stock) {
        cur = 0;
        String[] tstock = stock.split(",");
        String[] temp;
        String[] spl;
        String res;
        stocks = new loadData[50];
        try {
            for (count = 0; count < tstock.length; count++) {
                String url = "http://hq.sinajs.cn/list=" + tstock[count];
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        new URL(url).openConnection().getInputStream(), "GB2312"));
                while ((res = in.readLine()) != null) {
                    spl = res.split("\"");
                    if (spl[1].equals("FAILED") || spl[1].equals("")) {
                        stocks[count] = new loadData(tstock[count]);
                    } else {
                        temp = spl[1].split(",");
                        String[] bq = {temp[10], temp[12], temp[14], temp[16], temp[18]};
                        String[] bp = {temp[11], temp[13], temp[15], temp[17], temp[19]};
                        String[] sq = {temp[20], temp[22], temp[24], temp[26], temp[28]};
                        String[] sp = {temp[21], temp[23], temp[25], temp[27], temp[29]};
                        stocks[count] = new loadData(spl[0].substring(11, 19), temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[8], temp[9], bq, bp, sq, sp);
                    }
                }
            }
            sortStock();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
    }

    public static void sortStock(){
        loadData t;
        for(int i = 0; i < count; i++){
            for(int j = i + 1; j < count; j++){
                if((stocks[i].perc < stocks[j].perc && !stocks[j].name.equals("Error Stock")) || stocks[i].name.equals("Error Stock")){
                    if(cur == i){
                        cur = j;
                    }
                    else if(cur == j){
                        cur = i;
                    }
                    t = stocks[i];
                    stocks[i] = stocks[j];
                    stocks[j] = t;
                }
            }
        }
    }
}
